package com.overoid.diary.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;

import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.overoid.diary.R;
import com.overoid.diary.ui.common.Constants;
import com.overoid.diary.ui.common.ListActivityImpl;
import com.overoid.diary.ui.common.DBHelper;


public class MemoActivity extends ListActivityImpl {
	
	private DBHelper db;
	
	private String searchWord;
	private int memo_class_id;
	private int tatalCnt=0;
	private int pageNum=1;
	private int pageSize=0;
	private Cursor c;
	private ArrayList <MemoListItem> memoArray;
	private HashMap <Integer,Integer> deleteMemoMap;
	
	private String deleteWhere="";
	
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.memo);
		
		db = new DBHelper(getApplicationContext());
		memoArray = new ArrayList<MemoListItem>();
		deleteMemoMap = new HashMap<Integer,Integer>();
		
		//
		Intent in = getIntent();
		searchWord =  (in.getStringExtra("searchWord") ==null)?"":in.getStringExtra("searchWord");
		memo_class_id =  in.getIntExtra("memo_class_id",0);
		
		TextView searchTxt = (TextView)findViewById(R.id.memoTxtSearchWord);
		searchTxt.setText("Search Key Word : " + " "+searchWord+" ");
		
		c =db.getMemoList(memo_class_id,searchWord,1, 10,10);
		
		c.moveToFirst();
		
		for(int i=0; i < c.getCount(); i++) {
			MemoListItem m = new MemoListItem();
			
			m.memoId = c.getInt(0);
			m.memoContents = c.getString(1);
			m.memoClassId = c.getInt(2);
			m.memoClassName =c.getString(3);
			//m.memoClassBgImage =Integer.parseInt(c.getString(4));
			//Log.i("category=====================",c.getString(4) );
			m.memoRegDt =c.getString(5);
			
			memoArray.add(m);
			c.moveToNext();
		}
		
	
		if (c != null && !c.isClosed()) {
			c.close();
		}
		
		ImageButton categoryImgBtn=  null;
		
		// 하단 카테고리 셋팅 
		if(memo_class_id ==1){
			categoryImgBtn=  (ImageButton)findViewById(R.id.memoCategoryLove);
		}else if (memo_class_id ==2){
			categoryImgBtn=  (ImageButton)findViewById(R.id.memoCategoryMusic);
		}else if (memo_class_id ==3){
			categoryImgBtn=  (ImageButton)findViewById(R.id.memoCategorySad);
		}else if (memo_class_id ==4){
			categoryImgBtn=  (ImageButton)findViewById(R.id.memoCategoryTag);
		}
		
		if(categoryImgBtn != null){
			categoryImgBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.dayedit_picture_shape_select));
		}
		
				
		MemoListArrayAdapter adapter = new MemoListArrayAdapter(this, R.layout.memo_items,memoArray); 
		setListAdapter(adapter);		
		
		
		// search button click
		final ImageButton memoListBtnCancle =  (ImageButton)findViewById(R.id.memoListBtnCancle);
		memoListBtnCancle.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		// category love click
		final ImageButton memoCategoryLove =  (ImageButton)findViewById(R.id.memoCategoryLove);
		memoCategoryLove.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				categorySearch(1);
			}
		});
		
		// category Music click
		final ImageButton memoCategoryMusic =  (ImageButton)findViewById(R.id.memoCategoryMusic);
		memoCategoryMusic.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				categorySearch(2);
			}
		});
		
		// category Sad click
		final ImageButton memoCategorySad =  (ImageButton)findViewById(R.id.memoCategorySad);
		memoCategorySad.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				categorySearch(3);
			}
		});
		
		// category Tag click
		final ImageButton memoCategoryTag =  (ImageButton)findViewById(R.id.memoCategoryTag);
		memoCategoryTag.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				categorySearch(4);
			}
		});
		
		
		
		// 삭제
		final ImageButton memoListBtnDelete =  (ImageButton)findViewById(R.id.memoListBtnDelete);
		memoListBtnDelete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				
				if(deleteMemoMap.size() >0){ // 삭제 할게 있을때
				
					new AlertDialog.Builder(MemoActivity.this).setTitle("삭제").setMessage("삭제하시겠습니까?")
							.setPositiveButton("OK",
							new DialogInterface.OnClickListener() {
								
								@Override
								public void onClick(DialogInterface dialog, int which) {
									
									Iterator<Integer> iterVal = deleteMemoMap.values().iterator();
									
									deleteWhere = "(";
									while (iterVal.hasNext()) {
										int item = iterVal.next();
										if(iterVal.hasNext())
											deleteWhere = deleteWhere+item+",";
										else
											deleteWhere = deleteWhere+item;
									}
									
									deleteWhere = deleteWhere+")";
									
									Log.d("deleteWhere+++++++++++",deleteWhere);
									
									db.deleteMemoMedia(deleteWhere);
									db.deleteMemo(deleteWhere);
									finish();
									
								}
							}).setNegativeButton("Cancle",
							new DialogInterface.OnClickListener() {
								
								@Override
								public void onClick(DialogInterface dialog, int which) {
									
								}
							}).show();
					
					
						
				}else{
					Toast.makeText(MemoActivity.this,"삭제 목록이 없습니다.",Toast.LENGTH_SHORT).show();
				}
				
				
				
			}
		});
		
		
	}
	
	

	
	
	
	private class MemoListArrayAdapter extends ArrayAdapter<MemoListItem> {

		private ArrayList<MemoListItem> items;
		

		public MemoListArrayAdapter(Context context, int textViewResourceId, ArrayList<MemoListItem> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
		
			View v = convertView;
			
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.memo_items, null);
			}
			
			MemoListItem o = items.get(position);
			if (o != null) {
				((TextView) v.findViewById(R.id.memoItemTxtTop)).setText(o.memoRegDt);
				((TextView) v.findViewById(R.id.memoItemTxtBottom)).setText(o.memoContents);
				
				ImageView memoCategoryImg =  (ImageView)v.findViewById(R.id.memoCategoryImg);
				memoCategoryImg.setImageDrawable(getResources().getDrawable(Constants.getCategoryImage(o.memoClassId ) ));
				
				
				
				CheckBox memoListCheck =  (CheckBox)v.findViewById(R.id.memoListCheck);
				memoListCheck.setTag(new DeleteItem(position,items.get(position).memoId));
				memoListCheck.setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View v) {
						CheckBox cb= (CheckBox)v;
						DeleteItem item = (DeleteItem)cb.getTag();
						
						if(cb.isChecked()){
							deleteMemoMap.put(item.position, item.memoId);
						}else{
							
							deleteMemoMap.remove(item.position);
						}
						
					}
				});
				
				
			
			
				Log.i("memoId=====================",""+items.get(position).memoId );
				//String memo_id = new String(""+items.get(ItemPosition).memoId);
				LinearLayout memoListLinearContents =  (LinearLayout)v.findViewById(R.id.memoListLinearContents);
				memoListLinearContents.setTag(items.get(position).memoId);
				memoListLinearContents.setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View v) {
						Intent in = new Intent(MemoActivity.this, DayActivity.class);
						Log.i("memoListLinearContents=====================",""+Integer.parseInt(((LinearLayout)v).getTag().toString()));
						
						in.putExtra("memo_id", Integer.parseInt(((LinearLayout)v).getTag().toString()));
						startActivity(in);
					}
				});
			}
			return v;
		}
	}
	

	
	class MemoListItem {

		int memoId;
		String memoContents;
		int memoClassId;
		String memoClassName;
		int memoClassBgImage;
		String memoRegDt;
		
		public int getMemoId() {
			return memoId;
		}
		public void setMemoId(int memoId) {
			this.memoId = memoId;
		}
		public String getMemoContents() {
			return memoContents;
		}
		public void setMemoContents(String memoContents) {
			this.memoContents = memoContents;
		}
		public int getMemoClassId() {
			return memoClassId;
		}
		public void setMemoClassId(int memoClassId) {
			this.memoClassId = memoClassId;
		}
		public String getMemoClassName() {
			return memoClassName;
		}
		public void setMemoClassName(String memoClassName) {
			this.memoClassName = memoClassName;
		}
		public int getMemoClassBgImage() {
			return memoClassBgImage;
		}
		public void setMemoClassBgImage(int memoClassBgImage) {
			this.memoClassBgImage = memoClassBgImage;
		}
		public String getMemoRegDt() {
			return memoRegDt;
		}
		public void setMemoRegDt(String memoRegDt) {
			this.memoRegDt = memoRegDt;
		}
		
		
		
	}
	
	class DeleteItem {
		
		int position;
		int memoId;
		
		DeleteItem(int position ,int memoId){
			this.position = position;
			this.memoId = memoId;
		}

		
	}
	
	
	public void categorySearch(int selectPosition){
		
		if(memo_class_id != selectPosition){
			Intent in = new Intent(MemoActivity.this, MemoActivity.class);
			in.putExtra("searchWord", searchWord);
			in.putExtra("memo_class_id", selectPosition);
			startActivity(in);
			finish();
		}
				
	}
}


